<?php
namespace app\task\controller;
use app\task\logic\DealMistakeLogic;

/**
 * 处理知错能改
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/08
 * Time: 15:45
 */
class DealMistake extends Base {

    public function __construct(){
        parent::__construct();
        $this->dcb_logic = new DealMistakeLogic();
    }

    /**
     * 处理社区巡店分部知错能改
     * @return \think\response\Json
     */
    public function deal_cp_branch_mistake(){
        $maps = [];
        $maps[] = ['key','=','cp_time'];
        $conf_info = $this->dcb_logic->get_conf_info($maps);
        $cp_time = json_decode($conf_info['cp_time'],true);
        $code_arr = array_column($cp_time,null,'code');

        $now_date = date('Y-m-d');

        $maps = [];
        $branchids = model('branch')->get_column($maps,'id');
        foreach($branchids as $k_1 => $v_1){
            $maps = [];
            $maps[] = ['branchid','=',$v_1];
            $maps[] = ['time_code','<>','other'];
            $maps[] = ['now_date','<',$now_date];
            $maps[] = ['holiday','=',0];
            $maps[] = ['status','=',0];
            $cb_list = model('cp_branch')->get_list($maps,'id,branchid,time_code,now_date,status');

            $temp_arr = [];
            foreach($cb_list as $k_2 => $v_2){
                $flag = 'xd_' . $v_2['branchid'] . '_' . $v_2['time_code'] . '_' . $v_2['now_date'];

                $maps = [];
                $maps[] = ['flag','=',$flag];
                $m_detail = model('mistake')->get_detail($maps,'id');
                if(empty($m_detail)){
                    //查找分部
                    $maps = [];
                    $maps[] = ['id','=',$v_2['branchid']];
                    $b_detail = model('branch')->get_detail($maps,'id,name');

                    $temp_arr[$k_2]['content'] = $b_detail['name'] . '未完成' . $code_arr[$v_2['time_code']]['code_zw'] . '巡店';
                    $temp_arr[$k_2]['flag'] = $flag;
                    $temp_arr[$k_2]['now_date'] = $v_2['now_date'];
                    $temp_arr[$k_2]['top_out_time'] = date('Y-m-d',strtotime('+1 week',strtotime($v_2['now_date'])));
                }
            }
            $r = model('mistake')->insertAll($temp_arr);
        }
        return json($this->dcb_logic->get_response(1,'处理社区巡店分部知错能改，日期：' . $now_date));
    }


    /**
     * 处理事项超时知错能改
     * @return \think\response\Json
     */
    public function deal_matter_out_time_mistake(){
        $now_time = date('Y-m-d H:i:s');
        $maps = [];
        $maps[] = ['out_time','<>','0000-00-00 00:00:00'];
        $maps[] = ['out_time','<',$now_time];
        $maps[] = ['status','=',2];
        $list = model('matter')->get_list($maps,'id,sourceid,source,name,out_time,deal_openid');

        $temp_arr = [];
        foreach($list as $k => $v){
            $now_date = date('Y-m-d',strtotime($v['out_time']));
            $flag = 'sx_' . $v['sourceid'] . '_' . $v['source'] . '_' . $now_date;

            $maps = [];
            $maps[] = ['flag','=',$flag];
            $m_detail = model('mistake')->get_detail($maps,'id');
            if(empty($m_detail)){
                $content = str_replace('-','',$v['name']);

                //查找用户
                $maps = [];
                $maps[] = ['openid','=',$v['deal_openid']];
                $u_detail = model('user')->get_detail($maps,'id,openid,username');

                $temp_arr[$k]['openid'] = !empty($u_detail) ? $u_detail['openid'] : '';
                $temp_arr[$k]['content'] = $content . $u_detail['username'] . '超时未处理';
                $temp_arr[$k]['flag'] = $flag;
                $temp_arr[$k]['now_date'] = $now_date;
                $temp_arr[$k]['top_out_time'] = date('Y-m-d',strtotime('+1 week',strtotime($now_date)));
            }
        }

        $r = model('mistake')->insertAll($temp_arr);
        return json($this->dcb_logic->get_response(1,'处理事项超时知错能改，日期：' . $now_time));
    }
}
